/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package shaman.storage.impl.filter;

import java.awt.Frame;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.InvalidParameterException;
import java.util.Arrays;
import java.util.List;
import shaman.storage.api.Filter;
import shaman.storage.impl.filter.published.BufferPreprocessor;

/**
 *
 * @author dmitriy
 */
public abstract class BufferFilter implements Filter {
    public static final String PROPERTY_BUFFER_SIZE = "Размер буфера";
    
    private int bufferSize=32768;

    public BufferFilter() {
    }

    public abstract FilterType getFilterType();

    public InputStream getInputStream(InputStream compressedStream) throws IOException {
        return new BufferedInputStream(compressedStream, bufferSize);
    }

    public abstract String getName();

    public OutputStream getOutputStream(OutputStream compressedStream) throws IOException {
        return new BufferedOutputStream(compressedStream, bufferSize);
    }

    public List<String> getPropertyNames() {
        return Arrays.asList(new String[]{BufferPreprocessor.PROPERTY_BUFFER_SIZE});
    }

    public String getPropertyValue(String name) throws InvalidParameterException {
        if (BufferPreprocessor.PROPERTY_BUFFER_SIZE.equals(name)) {
            return String.valueOf(bufferSize);
        } else {
            throw new InvalidParameterException("Неизвестное свойство: " + name);
        }
    }

    public List<String> getPropertyValues(String name) throws InvalidParameterException {
        if (BufferPreprocessor.PROPERTY_BUFFER_SIZE.equals(name)) {
            return null;
        } else {
            throw new InvalidParameterException("Неизвестное свойство: " + name);
        }
    }

    public boolean hasConfigForm() {
        return false;
    }

    public void setPropertyValue(String name, String value) throws InvalidParameterException {
        if (BufferPreprocessor.PROPERTY_BUFFER_SIZE.equals(name)) {
            int newSize;
            try {
                newSize = Integer.parseInt(value);
            } catch (NumberFormatException ex) {
                throw new InvalidParameterException("Значение свойства должно быть числом");
            }
            if (newSize < 0) {
                throw new InvalidParameterException("Значение свойства должно быть положительным");
            }
            bufferSize = newSize;
        } else {
            throw new InvalidParameterException("Неизвестное свойство: " + name);
        }
    }

    public boolean showConfigForm(Frame parent) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

}
